import { defineStore } from "pinia"
import {
  reqCategory1List,
  reqCategory2List,
  reqCategory3List,
} from "@/api/category"
import type { categoryListType } from "@/api/category"

//当前state的类型
export interface categoryStateType {
  category1List: categoryListType
  category2List: categoryListType
  category3List: categoryListType
  category1Id: number | string
  category2Id: number | string
  category3Id: number | string
}

//定义category的store
const useCategoryStore = defineStore("category", {
  state: (): categoryStateType => {
    return {
      category1List: [],
      category2List: [],
      category3List: [],
      //收集一级分类的选择
      category1Id: "",
      //收集二级分类的选择
      category2Id: "",
      //收集二级分类的选择
      category3Id: "",
    }
  },
  actions: {
    //请求category1List的方法
    async getCategory1List() {
      const res = await reqCategory1List()
      this.category1List = res
    },
    //请求category2List的方法
    async getCategory2List() {
      const res = await reqCategory2List(this.category1Id as number)
      this.category2List = res
    },
    //请求category3List的方法
    async getCategory3List() {
      const res = await reqCategory3List(this.category2Id as number)
      this.category3List = res
    },
  },
})

export default useCategoryStore
